package com.gitee.wsl.transform.encode.sample

import com.gitee.wsl.ext.base.circleIndex
import com.gitee.wsl.ext.string.toByteArray
import com.gitee.wsl.transform.encode.DecodeToString
import com.gitee.wsl.transform.encode.StringEncode

import com.gitee.wsl.transform.encode.StringToStringEncodeDecode

class EmojiReplaceEncode(val shift: Int = 0):StringToStringEncodeDecode{
    override fun encode(src: String): String =
        src.asIterable().joinToString("") {
            emojiMap[EMOJI_BASE64_DICT.indexOf(it).circleIndex(emojiMap.size, shift)]
        }

    override fun decode(src: String): String =
        src.toByteArray(/*Charsets.UTF_32BE*/)
            .asIterable()
            .chunked(4)
            .map {
                EMOJI_BASE64_DICT[
                    (emojiMap
                        .indexOf(it.toByteArray().toString(/*Charsets.UTF_32BE*/))
                        .circleIndex(emojiMap.size, -shift))]
            }
            .joinToString("")


    /** emoji-aes */
    val emojiMap =
        listOf(
            "🍎",
            "🍌",
            "🏎",
            "🚪",
            "👁",
            "👣",
            "😀",
            "🖐",
            "ℹ",
            "😂",
            "🥋",
            "✉",
            "🚹",
            "🌉",
            "👌",
            "🍍",
            "👑",
            "👉",
            "🎤",
            "🚰",
            "☂",
            "🐍",
            "💧",
            "✖",
            "☀",
            "🦓",
            "🏹",
            "🎈",
            "😎",
            "🎅",
            "🐘",
            "🌿",
            "🌏",
            "🌪",
            "☃",
            "🍵",
            "🍴",
            "🚨",
            "📮",
            "🕹",
            "📂",
            "🛩",
            "⌨",
            "🔄",
            "🔬",
            "🐅",
            "🙃",
            "🐎",
            "🌊",
            "🚫",
            "❓",
            "⏩",
            "😁",
            "😆",
            "💵",
            "🤣",
            "☺",
            "😊",
            "😇",
            "😡",
            "🎃",
            "😍",
            "✅",
            "🔪",
            "🗒"
        )

   val EMOJI_BASE64_DICT = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/="

    companion object{
        val default = EmojiReplaceEncode()
    }
}

fun StringEncode.Companion.emojiReplace(shift: Int = 0) :StringEncode<String> = EmojiReplaceEncode(shift)

fun DecodeToString.Companion.emojiReplace(shift: Int = 0) :DecodeToString<String> = EmojiReplaceEncode(shift)

val StringEncode.Companion.emojiReplace : StringEncode<String>
    get() = EmojiReplaceEncode.default

val DecodeToString.Companion.emojiReplace : DecodeToString<String>
    get() = EmojiReplaceEncode.default